/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.entity.PatsInTransferring;
import com.jims.master.modules.doctor.service.MedicalRecordService;
import com.jims.master.modules.doctor.vo.TurnInVo;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.service.OrgGroupVsDeptService;
import com.jims.master.modules.sys.service.OrgGroupVsUserService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 医生病历流转Controller
 * @author CTQ
 * @version 2017年3月28日09:25:27
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/medicalRecord")
public class MedicalRecordController extends BaseController {

@Autowired
	private MedicalRecordService medicalRecordService;
	@Autowired
	private OrgGroupVsDeptService orgGroupVsDeptService;
	@Autowired
	private OrgGroupVsUserService orgGroupVsUserService;
	@Autowired
	private OrgDeptService orgDeptService;
	
	@ModelAttribute
	public PatVisit get(@RequestParam(required=false) String id) {
		PatVisit entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = medicalRecordService.get(id);
		}
		if (entity == null){
			entity = new PatVisit();
		}
		return entity;
	}


	/**
	 * @param turnInVo 传递参数
	 * @return String   返回类型
	* @throws
	 * @Title: turnInIndexDept
	 * @Desciption: (加载移入病历页面)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "turnInIndexDept")
	public String turnInIndexDept(TurnInVo turnInVo, Model model) {
		model.addAttribute("turnInVo", turnInVo);
		return "modules/doctor/medicalRecord/turnInIndexDept";
	}


	/**
	 * @param turnInVo 传递参数
	 * @return String    返回类型
	* @throws
	 * @Title: waitDeptPatientList
	 * @Desciption: 查询所有待移入当前科室病人病历)
	 * @author CTQ
	 * @date 2017-03-30
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"waitDeptPatientList", ""})
	public String waitDeptPatientList(TurnInVo turnInVo,Model model) {
		turnInVo.setOrgId(UserUtils.getOrgId());
		turnInVo.setDeptTransferedTo(UserUtils.getUser().getOrgDept().getId());
		List<TurnInVo> list = medicalRecordService.waitDeptPatientList(turnInVo);
		model.addAttribute("list", list);
		return "modules/doctor/medicalRecord/turnIn_newInDept";
	}

	/**
	 * @param turnInVo 传递参数
	 * @return String    返回类型
	 * @throws
	 * @Title: allwaitDeptPatientList
	 * @Desciption: 查全院所有待移入病人病历)
	 * @author CTQ
	 * @date 2017-03-30
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"allwaitDeptPatientList", ""})
	public String allwaitDeptPatientList(TurnInVo turnInVo,Model model) {
		turnInVo.setOrgId(UserUtils.getOrgId());
		List<TurnInVo> list = medicalRecordService.waitDeptPatientList(turnInVo);
		model.addAttribute("list", list);
		return "modules/doctor/medicalRecord/turnIn_turnInDept";
	}


	/**
	 * @param turnInVo 传递参数
	 * @return String    返回类型
	* @throws
	 * @Title: findInfoByParam
	 * @Desciption: (选择病人回填表单数据/根据病人身份证号查询病人)
	 * @author CTQ
	 * @date 2017-03-30
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "findInfoByParam")
	public String findInfoByParam(TurnInVo turnInVo,String type,Model model) {
		User user = UserUtils.getUser();
		model.addAttribute("user",user);
		if (StringUtils.isNotBlank(type)){
			return "modules/doctor/medicalRecord/turnIn_form";
		}else{
			turnInVo.setOrgId(user.getOrgId());
			//turnInVo.setDeptTransferedTo(user.getOrgDept().getId());

			TurnInVo vo=medicalRecordService.findInfoByParam(turnInVo);
			OrgDept dept = orgDeptService.get(user.getOrgDept().getId());
			if(vo!=null){
				vo.setDept(dept);
			}else {
				vo = new TurnInVo();
			}
			vo.setSituationDate(new Date());
			model.addAttribute("turnInVo",vo);
			/*根据科室查询科室下医生*/
			List<User> result= UserUtils.getRoleUserList(dept.getId());
			model.addAttribute("users",result);
			return "modules/doctor/medicalRecord/turnIn_form";
		}
	}
	/**
	 * @param turnInVo 传递参数
	 * @return String    返回类型
	 * @throws
	 * @Title: findInfoByParam
	 * @Desciption: (根据病人身份证号查询病人)
	 * @author CTQ
	 * @date 2017-07-25 10:23:49
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "validIdCard")
	@ResponseBody
	public StringData validIdCard(TurnInVo turnInVo,String type) {
		StringData stringData = new StringData();
		if (StringUtils.isNotBlank(type)){
		}else{
			turnInVo.setOrgId(UserUtils.getOrgId());
			TurnInVo vo=medicalRecordService.findInfoByParam(turnInVo);
			if(vo!=null){
				stringData.setCode("success");
			}else {
				stringData.setCode("error");
			}
		}
		return stringData;
	}
	/**
	 * @param turnInVo 传递参数
	 * @return StringData    返回类型
	 * @throws
	 * @Title: turnIn
	 * @Desciption: (医生新建/移入病人病历)
	 * @author CTQ
	 * @date 2017-03-28
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "turnIn")
	@ResponseBody
	public StringData save(TurnInVo turnInVo) {
		StringData data=new StringData();
		String i=medicalRecordService.turnInPatient(turnInVo);
		if(StringUtils.isNotBlank(i) && i.equals("0")){
			data.setCode("success");
			data.setData("新建病人病历成功");
		}else{
			data.setCode("error");
			data.setData("新建病人病历失败");
		}
		return data;
	}
	/**
	 * @param turnInVo 传递参数
	 * @return String   返回类型
	 * @throws
	 * @Title: turnOutDept
	 * @Desciption: (加载移出病历页面)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "cancelTurnInDept")
	public String cancelTurnInDept(TurnInVo turnInVo, Model model) {
		model.addAttribute("turnInVo", turnInVo);
		return "modules/doctor/medicalRecord/cancel_turnInIndex";
	}
	/**
	 * @param turnInVo 传递参数
	 * @return String   返回类型
	 * @throws
	 * @Title: findWaitTurnOutPatientList
	 * @Desciption: (查询所有待出病人列表)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"findWaitCancelTurnInPatientList", ""})
	public String findWaitCancelTurnInPatientList(TurnInVo turnInVo,Model model) {
		turnInVo.setOrgId(UserUtils.getOrgId());
		turnInVo.setDeptStayed(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
		List<TurnInVo> list = medicalRecordService.findWaitTurnOutPatientList(turnInVo);
		model.addAttribute("list", list);
		return "modules/doctor/medicalRecord/cancel_turnInList";
	}

	/**
	 * @param turnInVo 传递参数
	 * @return String    返回类型
	* @throws
	 * @Title: findInfoByCancelTurnInParam
	 * @Desciption: (查询要取消移出病历病人信息)
	 * @author CTQ
	 * @date 2017-04-01
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "findInfoByCancelTurnInParam")
	public String findInfoByCancelTurnInParam(TurnInVo turnInVo,Model model) {
		User user = UserUtils.getUser();
		turnInVo.setOrgId(user.getOrgId());
		turnInVo.setDeptTransferedTo(user.getOrgDept().getId());
		turnInVo.setSituationDate(new Date());
		TurnInVo vo=medicalRecordService.findInfoByTurnOutParam(turnInVo);
		OrgDept dept = orgDeptService.get(user.getOrgDept().getId());
		if (vo!=null){
			vo.setDept(dept);
			vo.setTransferDate(new Date());
		}else {
			vo = new TurnInVo();
		}
		model.addAttribute("turnInVo",vo);
		/*根据科室查询科室下医生*/
		List<User> result= UserUtils.getRoleUserList(dept.getId());
		model.addAttribute("users",result);
		return "modules/doctor/medicalRecord/cancel_turnInForm";

	}

	/**
	 * @param turnInVo 传递参数
	 * @return StringData    返回类型
	* @throws
	 * @Title: saveCancleTurnIn
	 * @Desciption: (取消移入病历)
	 * @author CTQ
	 * @date 2017-04-01
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "saveCancleTurnIn")
	@ResponseBody
	public StringData saveCancleTurnIn(TurnInVo turnInVo) {
		return medicalRecordService.cancelTurnInPatient(turnInVo);
	}

	/**
	 * @param turnInVo 传递参数
	 * @return String   返回类型
	 * @throws
	 * @Title: turnOutDept
	 * @Desciption: (加载移出病历页面)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "turnOutDept")
	public String turnOutDept(TurnInVo turnInVo, Model model) {
		model.addAttribute("turnInVo", turnInVo);
		return "modules/doctor/medicalRecord/turnOutIndex";
	}
	/**
	 * @param turnInVo 传递参数
	 * @return String   返回类型
	 * @throws
	 * @Title: findWaitTurnOutPatientList
	 * @Desciption: (查询所有待出病人列表)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"findWaitTurnOutPatientList", ""})
	public String findWaitTurnOutPatientList(TurnInVo turnInVo,Model model) {
		turnInVo.setOrgId(UserUtils.getOrgId());
		turnInVo.setDeptStayed(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
		List<TurnInVo> list = medicalRecordService.findWaitTurnOutPatientList(turnInVo);
		model.addAttribute("list", list);
		return "modules/doctor/medicalRecord/turnOutList";
	}

	/**
	 * @param turnInVo 传递参数
	 * @return String   返回类型
	 * @throws
	 * @Title: findInfoByTurnOutParam
	 * @Desciption: (根据参数查询要移出病历病人信息)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "findInfoByTurnOutParam")
	public String findInfoByTurnOutParam(TurnInVo turnInVo,Model model) {
		User user = UserUtils.getUser();
		turnInVo.setOrgId(user.getOrgId());
		turnInVo.setDeptTransferedFrom(user.getOrgDept().getId());
		turnInVo.setSituationDate(new Date());
		TurnInVo vo=medicalRecordService.findInfoByTurnOutParam(turnInVo);
		OrgDept dept = orgDeptService.get(user.getOrgDept().getId());
		if (vo!=null){
			vo.setDept(dept);
			vo.setTransferDate(new Date());
		}else {
			vo = new TurnInVo();
		}
		model.addAttribute("turnInVo",vo);
		/*根据科室查询科室下医生*/
		List<User> result= UserUtils.getRoleUserList(dept.getId());
		model.addAttribute("users",result);
		return "modules/doctor/medicalRecord/turnOutForm";

	}
	/**
	 * @param turnInVo 传递参数
	 * @return StringData    返回类型
	 * @throws
	 * @Title: turnOutPatient
	 * @Desciption: (移出病人病历)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "saveTurnOutPatient")
	@ResponseBody
	public StringData turnOutPatient(TurnInVo turnInVo) {
		return medicalRecordService.turnOutPatient(turnInVo);
	}
	/**
	 * @param turnInVo 传递参数
	 * @return String   返回类型
	 * @throws
	 * @Title: cancelTurnOutDept
	 * @Desciption: (加载取消移出病历页面)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "cancelTurnOutDept")
	public String cancelTurnOutDept(TurnInVo turnInVo, Model model) {
		model.addAttribute("turnInVo", turnInVo);
		return "modules/doctor/medicalRecord/cancel_turnOutIndex";
	}

	/**
	 * @param turnInVo 传递参数
	 * @return String   返回类型
	* @throws
	 * @Title: findWaitCancelPatientList
	 * @Desciption: (查询所有待取消转出病人列表)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"findWaitCancelPatientList", ""})
	public String findWaitCancelPatientList(TurnInVo turnInVo,Model model) {
		turnInVo.setOrgId(UserUtils.getOrgId());
		turnInVo.setDeptTransferedFrom(UserUtils.getUser().getOrgDept().getId());//从本科室转出病人
		//turnInVo.setDeptTransferedTo(UserUtils.getUser().getOrgDept().getId());/**此处应该查询当前用户的所有权限科室下人员**/
		List<TurnInVo> list = medicalRecordService.findWaitCancelPatientList(turnInVo);
		model.addAttribute("list", list);
		return "modules/doctor/medicalRecord/cancel_turnOutList";
	}

	/**
	 * @param turnInVo 传递参数
	 * @return String   返回类型
	* @throws
	 * @Title: findInfoByCancelParam
	 * @Desciption: (根据参数查询病人信息)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "findInfoByCancelParam")
	public String findInfoByCancelParam(TurnInVo turnInVo,Model model) {
		User user = UserUtils.getUser();
		turnInVo.setOrgId(user.getOrgId());
		turnInVo.setDeptTransferedTo(user.getOrgDept().getId());
		turnInVo.setSituationDate(new Date());
		TurnInVo vo=medicalRecordService.findInfoByCancelParam(turnInVo);
		OrgDept dept = orgDeptService.get(user.getOrgDept().getId());
		vo.setDept(dept);
		model.addAttribute("turnInVo",vo);
		/*根据科室查询科室下医生*/
		List<User> result= UserUtils.getRoleUserList(dept.getId());
		model.addAttribute("users",result);
		return "modules/doctor/medicalRecord/cancel_turnOutForm";

	}

	/**
	 * @param turnInVo 传递参数
	 * @return StringData    返回类型
	* @throws
	 * @Title: cancleTurnOutPatient
	 * @Desciption: (取消移出病人病历)
	 * @author CTQ
	 * @date 2017-03-31
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "saveCancleTurnOutPatient")
	@ResponseBody
	public StringData saveCancleTurnOutPatient(TurnInVo turnInVo) {
		StringData data=new StringData();
		String i=medicalRecordService.cancleTurnOutPatient(turnInVo);
		if(StringUtils.isNotBlank(i) && i.equals("0")){
			data.setCode("success");
			data.setData("取消移出病人病历成功");
		}else{
			data.setCode("error");
			data.setData("取消移出病人病历失败");
		}
		return data;
	}
}